﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace BalanceManager.Model
{
    public class BalanceManagerDataContext : DataContext
    {
        public static string DBConnectionString = "Data Source=isostore:/database.sdf;";

        public BalanceManagerDataContext(string connectionString) : base(connectionString)
        {

        }

        public Table<Item> Items;
        public Table<FundFlow> FundFlows;
    }

    public class BaseTable : INotifyPropertyChanged, INotifyPropertyChanging
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangingEventHandler PropertyChanging;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }
    }

    [Table]
    public class Item : BaseTable
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id { get; set; }

        private string name;

        [Column]
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    NotifyPropertyChanging("Name");
                    name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        private int? parentId;
        [Column]
        public int? ParentId
        {
            get { return parentId; }
            set
            {
                if (parentId != value)
                {
                    NotifyPropertyChanging("ParentId");
                    parentId = value;
                    NotifyPropertyChanged("ParentId");
                }
            }
        }

        private int updateFrequency;
        [Column(CanBeNull = false)]
        public int UpdateFrequency
        {
            get { return updateFrequency; }
            set
            {
                if (updateFrequency != value)
                {
                    NotifyPropertyChanging("UpdateFrequency");
                    updateFrequency = value;
                    NotifyPropertyChanged("UpdateFrequency");
                }
            }
        }

        public override string ToString()
        {
            return Name;
        }
    }

    [Table]
    public class FundFlow : BaseTable
    {
        public FundFlow()
        {
            Date = DateTime.Now;
        }

        public FundFlow(FlowType type)
        {
            Date = DateTime.Now;
            FlowType = type;
        }

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id { get; set; }


        public string ItemName { get; set; }

        private decimal amount;

        [Column]
        public decimal Amount
        {
            get { return amount; }
            set
            {
                if (amount != value)
                {
                    NotifyPropertyChanging("Amount");
                    amount = value;
                    NotifyPropertyChanged("Amount");
                }
            }
        }

        private int itemId;

        [Column]
        public int ItemId
        {
            get { return itemId; }
            set
            {
                if (itemId != value)
                {
                    NotifyPropertyChanging("ItemId");
                    itemId = value;
                    NotifyPropertyChanged("ItemId");
                }
            }
        }

        private FlowType flowType;

        [Column]
        public FlowType FlowType
        {
            get { return flowType; }
            set
            {
                if (flowType != value)
                {
                    NotifyPropertyChanging("FlowType");
                    flowType = value;
                    NotifyPropertyChanged("FlowType");
                }
            }
        }

        private DateTime date;

        [Column]
        public DateTime Date
        {
            get { return date; }
            set
            {
                if (date != value)
                {
                    NotifyPropertyChanging("Date");
                    date = value;
                    NotifyPropertyChanged("Date");
                }
            }
        }
    }
}
